<HTML>
<HEAD>
<TITLE>[Chapter 16] 16.2 Transferable Interface</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:56:59 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch16_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 16<br>Data Transfer</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch16_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-16-SECT-2">16.2 Transferable Interface</A></h2>

<P CLASS=para>
Objects that can be placed on a clipboard 
must implement the <tt CLASS=literal>Transferable</tt> 
interface. This interface defines a number of methods that let an object 
describe how it presents itself to clipboard 
readers. That sounds complex, but it isn't really; these methods 
let a clipboard reader find out what data flavors are available and what 
Java types they represent. 

<P CLASS=para>
The significance of the <tt CLASS=literal>Transferable</tt> 
interface is that it provides a way to get information about the object 
on the clipboard without knowing what the object actually is. When you 
read the clipboard, you don't necessarily know what kind of object 
is there. It might be some kind of text string, but it could just as likely 
be something bizarre. However, you shouldn't have to care. If you're 
looking for a <tt CLASS=literal>String</tt>, you care only that the object exists in a <tt CLASS=literal>stringFlavor</tt> 
representation. These methods let you ask the object 
what flavors it supports. 

<P CLASS=para>
For text strings, the data transfer package provides a <tt CLASS=literal>StringSelection</tt> 
class that implements <tt CLASS=literal>Transferable</tt>. 
At this point, if you want to transfer other kinds of objects, you'll 
have to create a class that implements <tt CLASS=literal>Transferable</tt> 
yourself. It wouldn't be unreasonable for JavaSoft to provide other 
"selection" classes (for example, <tt CLASS=literal>ImageSelection</tt>) 
in the future. Methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public abstract DataFlavor[] getTransferDataFlavors() <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getTransferDataFlavors()</tt> 
method should return a sorted array of <tt CLASS=literal>DataFlavor</tt>s 
that you support. The most descriptive flavor should be the first element 
in the array and the least descriptive, last. For example, a textual object 
would place <tt CLASS=literal>DataFlavor.plainTextFlavor</tt> 
last, because it has less information than <tt CLASS=literal>DataFlavor.stringFlavor</tt> 
(which includes information like the length of the string) and much less 
information than a hypothetical flavor like <tt CLASS=literal>DataFlavor.richTextFlavor</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public abstract boolean isDataFlavorSupported(DataFlavor flavor) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isDataFlavorSupported()</tt> 
method should return <tt CLASS=literal>true</tt> if 
the object supports the given <tt CLASS=literal>flavor</tt> 
and <tt CLASS=literal>false</tt> otherwise. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public abstract Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getTransferData()</tt> method 
is the most complicated to implement. It should return an instance of the 
class representing the data in the given <tt CLASS=literal>flavor</tt>. 
If <tt CLASS=literal>flavor</tt> is not supported 
by this object, <tt CLASS=literal>getTransferData()</tt> 
must throw the <tt CLASS=literal>UnsupportedFlavorException</tt>. 
However, this method must be able to return a class for each flavor the 
object supports (i.e., each data flavor listed by <tt CLASS=literal>getTransferDataFlavors()</tt>). 
The method could throw an <tt CLASS=literal>IOException</tt> 
when returning with a <tt CLASS=literal>Reader</tt> 
as the representation class. For example, if some data flavor required 
you to return a <tt CLASS=literal>FileReader</tt> 
and the file doesn't exist, this method might throw an <tt CLASS=literal>IOException</tt>. </DL>
</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch16_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch16_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>DataFlavor</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>ClipboardOwner Interface</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
